package com.cvmars.jianyou.module.base;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.config.MyConfig;
import com.cvmars.jianyou.model.ImageModel;
import com.cvmars.jianyou.model.ImageUploadModel;
import com.cvmars.jianyou.utils.ImageUtils;
import com.cvmars.jianyou.utils.LogUtils;
import com.cvmars.jianyou.utils.ToastUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


//图片预览页面
public class BaseImagePagerActivity extends BaseActivity {


    @BindView(R.id.base_viewpager)
    ViewPager viewPager;

    @BindView(R.id.txt_page_position)
    TextView txtPagePosition;
    List<ImageUploadModel> ImgModels;
    @BindView(R.id.txt_download)
    TextView txtDownload;

    int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_image_pager);
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);
        ImgModels = (List<ImageUploadModel>) getIntent().getSerializableExtra(MyConfig.INTENT_DATA_URL);
        position = getIntent().getIntExtra("position",0);

        SamplePagerAdapter pagerAdapter = new SamplePagerAdapter();
        viewPager.setAdapter(pagerAdapter);
        if (ImgModels != null) {
            txtPagePosition.setText("1/" + ImgModels.size());
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                if (ImgModels != null) {
                    txtPagePosition.setText((arg0 + 1) + "/" + ImgModels.size());
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        viewPager.setCurrentItem(position);
    }

    @SuppressLint("StaticFieldLeak")
    @OnClick(R.id.txt_download)
    public void onViewClicked() {

        new AsyncTask<String, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(String... strings) {

                Bitmap bitmap = ImageUtils.GetImageInputStream(strings[0]);
                boolean base = ImageUtils.SavaImage(bitmap, Environment.getExternalStorageDirectory().getPath() + "/Test");
                return base;
            }


            @Override
            protected void onPostExecute(Boolean isSave) {
                super.onPostExecute(isSave);
                if (isSave) {
                    ToastUtils.show("下载成功");
                }
            }
        }.execute(ImgModels.get(viewPager.getCurrentItem()).url);
    }


    class SamplePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (ImgModels != null) {
                return ImgModels.size();
            }
            return 0;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            final PhotoView photoView = new PhotoView(container.getContext());
            final String path = ImgModels.get(position).url;
            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {

                    finish();

                }
            });
            LogUtils.d("i:" + position);

            container.addView(photoView);
            ImageUtils.loadImage(BaseImagePagerActivity.this, path, photoView);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}
